<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .wrapper {
        width: 600px;
        margin: 10px auto;
        border: 5px solid pink;
      }
      ul {
        overflow: hidden;
      }
      ul li {
        list-style-type: none;
        float: left;
        margin-right: 10px;
      }
      ul li img {
        width: 140px;
        cursor: pointer;
      }
      h2 {
        margin-bottom: 10px;
        text-align: center;
      }
      .wrapper > img {
        /* >只找子元素*/
        width: 100%;
      }
      .wrapper p {
        font-size: 28px;
        color: aqua;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <div class="wrapper">
      <h2>相册</h2>
      <hr />
      <ul>
        <li><img class="photo-item" src="../images/05-1-small.jpg" /></li>
        <li><img class="photo-item" src="../images/05-2-small.jpg" /></li>
        <li><img class="photo-item" src="../images/05-3-small.jpg" /></li>
        <li><img class="photo-item" src="../images/05-4-small.jpg" /></li>
      </ul>
      <img class="large-img" src="../images/05-placeholder.png" />
      <p>请选择图片</p>
    </div>
  </body>
  <script>
    var img = document.querySelector(".large-img");
    var items = document.querySelectorAll(".photo-item");
    var p = document.querySelector("p");
    for (var i in items) {
      items[i].onmouseover = function () {
        //onclick/onmouseenter
        var src = this.getAttribute("src").replace("-small", "");
        img.setAttribute("src", src);
        var idx = src.substr(13, 1);//字符串裁剪
        var str = "";
        switch (idx) {
          case "1":
            str = "图片A";
            break;
          case "2":
            str = "图片B";
            break;
          case "3":
            str = "图片C";
            break;
          case "4":
            str = "图片D";
            break;
        }
        p.innerText = str;
      };
    }
  </script>
</html>
